package theOrbalEra.common.network;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import theOrbalEra.common.item.OrbalItemEleBag;
import theOrbalEra.common.util.DefultValue;
import theOrbalEra.common.util.idList.PacketId;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.network.packet.Packet250CustomPayload;

import com.google.common.io.ByteArrayDataInput;

import cpw.mods.fml.common.network.PacketDispatcher;

public class PacketDeBag extends PacketBase{
	public static PacketDeBag instance =new PacketDeBag();

	@Override
	void readClient(int id, ByteArrayDataInput data, Object[] extradata) {
		
		
	}

	@Override
	void readServer(int id, ByteArrayDataInput data, Object[] extradata) {
		int wei=data.readInt();
		int[] bag=new int[7];
		for(int i=0;i<7;i++)
			bag[i]=data.readInt();
		EntityPlayer player=(EntityPlayer) extradata[0];
		 
		deBag(wei, bag,player);
		
	}
	
	private void deBag(int wei, int[] bag, EntityPlayer player) {
		OrbalItemEleBag.setEleCount(player.inventory.mainInventory[wei], bag);
		
	}

	public static void deBag(int wei,int[] bag){
		ByteArrayOutputStream bos = new ByteArrayOutputStream(140);
        DataOutputStream dos = new DataOutputStream(bos);

        try
        {
        	dos.writeInt(PacketId.DE_BAG);
            dos.writeInt(wei);
            for(int i:bag)
            	dos.writeInt(i);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        Packet250CustomPayload pkt = new Packet250CustomPayload();
        pkt.channel = DefultValue.channel;
        pkt.data = bos.toByteArray();
        pkt.length = bos.size();
        pkt.isChunkDataPacket = false;
        PacketDispatcher.sendPacketToServer(pkt);
		
	}


}
